<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Verify and save data from editorpage and then call categorypage
 * used from category.page.php
 *
 * @author Norbert Gebert
 * @copyright Norbert Gebert 25.09.2011
 * @license GPL3
 *
 * @version 0.5b
 * @package Admin
 * @subpackage page
 */
// Check the validity of all entries and save it in the mysql-string to update
// if not Valid add a entry to the errorMessage-variable.
$mysqlBuffer = array();
// Test of exist record
$datax = $refValidate->testNumber($_GET['id'], '');
if (!$datax)
{
	message::setErrorRow(_('ID-Number for saving has a wrong format or is invalid.') .
		'<br>' . _('No data could be saved!'), true);
}
else
{
	$idx = $datax;

	//Categoryname
	$datax = $refValidate->testText($_POST['namex'], _('Categoryname'), 3, 64);
	if ($datax or ! empty($datax))
	{
		$catNamex = $idx . ' ' . $datax;
		$mysqlBuffer['category_name'] = $datax;
	}

	// Category desciption
	$mysqlBuffer['category_description'] = $_POST['textx'];

	// Image
	$datax = $refValidate->cleanslash($_FILES['uploadfile']['name']);

	if ($datax and ! empty($datax))
	{ // First delete old Image if available
		$oldImagex = $refValidate->testText($_POST['oldImagex'], _('Category image'));
		$file = '../images/' . CATEGORY_IMAGE_PATH . '/' . $idx . '/' . $oldImagex;

		if (!empty($oldImagex) and file_exists($file))
		{
			if (unlink('../images/' . CATEGORY_IMAGE_PATH . '/' . $idx . '/' .
					$oldImagex))
			{
				message::setNoteRow(_('The old image file was replaced successfully.'));
			}
		}
		if (!is_dir('../images/' . CATEGORY_IMAGE_PATH))
		{
			mkdir('../images/' . CATEGORY_IMAGE_PATH);
		}
		if (!is_dir('../images/' . CATEGORY_IMAGE_PATH . '/' . $idx))
		{
			mkdir('../images/' . CATEGORY_IMAGE_PATH . '/' . $idx);
		}
		include_once('module/class/upload.class.php');
		$upload = new upload;
		if ($upload->doUpload($datax, CATEGORY_IMAGE_PATH . '/' . $idx, CATEGORY_IMAGE_SIZE,
				'uploadfile'))
		{
			$mysqlBuffer['category_image'] = $datax;
		}
		else
		{
			message::setErrorRow($upload->getError());
		}
	}

	// Small-image
	$datax = $refValidate->cleanslash($_FILES['smallUploadfile']['name']);

	if ($datax and ! empty($datax))
	{ // First delete old Small-Image if available
		$oldSmallImagex = $refValidate->testText($_POST['oldSmallImagex'],
			_('Small category image'));
		$file = '../images/' . CATEGORY_IMAGE_PATH . '/' . $idx . '/' .
			CATEGORY_IMAGE_THUMB_PATH . '/' . $oldSmallImagex;

		if (!empty($oldSmallImagex) and file_exists($file))
		{
			if (unlink('../images/' . CATEGORY_IMAGE_PATH . '/' . $idx . '/' .
					CATEGORY_IMAGE_THUMB_PATH . '/' . $oldSmallImagex))
			{
				message::setNoteRow(_('The old small image file was replaced successfully. '));
			}
		}
		if (!is_dir('../images/' . CATEGORY_IMAGE_PATH))
		{
			mkdir('../images/' . CATEGORY_IMAGE_PATH);
		}
		if (!is_dir('../images/' . CATEGORY_IMAGE_PATH . '/' . $idx))
		{
			mkdir('../images/' . CATEGORY_IMAGE_PATH . '/' . $idx);
		}
		if (!is_dir('../images/' . CATEGORY_IMAGE_PATH . '/' . $idx . '/' .
				CATEGORY_IMAGE_THUMB_PATH))
		{
			mkdir('../images/' . CATEGORY_IMAGE_PATH . '/' . $idx . '/' .
				CATEGORY_IMAGE_THUMB_PATH);
		}

		include_once('module/class/upload.class.php');
		include_once('module/class/upload.class.php');
		$upload = new upload;
		if ($upload->doUpload($datax,
				CATEGORY_IMAGE_PATH . '/' . $idx . '/' .
				CATEGORY_IMAGE_THUMB_PATH, CATEGORY_IMAGE_SIZE, 'smallUploadfile'))
		{
			$mysqlBuffer['category_small_image'] = $datax;
		}
		else
		{
			message::setErrorRow($upload->getError());
		}
	}

	// Category activ
	if (isset($_POST['activex']))
	{
		$mysqlBuffer['category_active'] = '1';
		;
	}
	else
	{
		$mysqlBuffer['category_active'] = '0';
	}

	// Category lastchange
	$mysqlBuffer['category_last_change'] = date("Y-m-d H:i:s");

	// Category browsepage
	$datax = $refValidate->testText($_POST['browsepagex'], _('Browsepage'), 1);
	if ($datax)
	{
		$mysqlBuffer['category_browsepage'] = $datax;
	}

	// Category top and category order
	// @Todo Verschachtelungstiefe prüfen / korrigieren
	// category order - if change replace old value
	$datax = $refValidate->testText($_POST['topx'], _('Category order'));
	if ($datax <> $refValidate->cleanslash($_POST['oldtopx']))
	{
		$orderx = sortOrder($datax, $refValidate->cleanslash($_POST['oldtopx']), $idx);
		if ($orderx >= 0)
		{
			$mysqlBuffer['category_order'] = strval($orderx);
		}
		else
		{
			message::setErrorRow(_('Category order are wrong or invalid.'), true);
		}
		$mysqlBuffer['category_top'] = $datax;
	}

	// Number of products to show
	$datax = $refValidate->cleanslash(intval($_POST['toshowx']));
	$number = intval($datax);
	if ($number < 0 or $number >= 1000)
	{
		message::setErrorRow(_('Number of products to display are wrong format or invalid.') .
			'<br>' . _('No data could be saved!'), true);
	}
	else
	{
		$catNamex = $idx . ' ' . $datax;
		$mysqlBuffer['category_display_products'] = $number;
	}

	// Category metadescription
	$datax = $refValidate->testText($_POST['metadescription'], _('Error in metadescription.'));
	if ($datax)
	{
		$mysqlBuffer['category_metadescription'] = $datax;
	}
	else
	{
		message::setWarningRow(_('No Metadescription indicated!'));
	}

	// Category metakeywords
	$datax = $refValidate->testText($_POST['metakeyword'], _('Error in metakeywords.'));
	if ($datax)
	{
		$mysqlBuffer['category_metakey'] = $datax;
	}
	else
	{
		message::setWarningRow(_('No Metakeywords indicated!'));
	}

	db::getInstance()->where('category_id', $idx);
	$result = db::getInstance()->getOne('category');
	if (db::getInstance()->count >= 0)
	{
		db::getInstance()->where('category_id', $idx);
		if (db::getInstance()->update('category', $mysqlBuffer))
		{
			message::setNoteRow(_('Data successful saved.'), TRUE);
		}
	}
	else
	{
		message::setErrorRow(_('Data coud not save!') . ' -> ' . $catNamex, true);
	}

	if (message::ifWarningMessage())
		message::setWarningRow(_('Warnings!') . ' -> ' . $catNamex, true);

	if (message::ifErrorMessage())
	{
		message::setErrorRow(_('A fault is data store this one appeared!') .
			' -> ' . $catNamex, true);
		$refPage->addtpl('content', '/' . $page . '/' . $subpage . '.editor.tpl.php');
	}
}
?>